<script type="text/javascript" src="<?php echo base_url('assets/js/admin/load.js'); ?>"></script>
<?php
if ($this->uri->segment(4) == '') {
    $editar = false;
} else {
    $editar = true;
}
$id_objeto = $this->uri->segment(4);
?>

<div class="conteudo">
    <div class="cabecalho">
        <h3>Cadastro de posts</h3>
    </div>
    <div class="inner no-padding full" >
        <div style="padding: 30px 30px 0;">
            <form action="<?php echo site_url('admin/post/save/' . $id_objeto); ?>" method="POST" id="form">
                <div>
                    Menu principal
                </div>

                <div class="overflow">
                    <?php
                    $c = new Post();
                    echo form_dropdown('id_menu_principal', $c->getMenusPrincipais(), $this->input->post('id_menu_principal'), 'id="id_menu_principal" class="left"');
                    ?>
                    <span id="container-load"class="left"></span>
                </div>
                <br/>
                <div>
                    Menu
                </div>
                <div class="overflow">
                    <?php
                    $id_menu_principal = $this->input->post('id_menu_principal');
                    include 'components' . DIRECTORY_SEPARATOR . 'combobox_menu.php';
                    ?>
                </div>
                <br/>
                <div>
                    Título
                </div>
                <div>
                    <input name="titulo" type="text" style="width: 100%;" value="<?php echo $this->input->post('titulo'); ?>" />
                </div>
                <br/>
                <div>
                    Ativo
                </div>
                <div class="left group-box">
                    <label for="ativo">Sim</label>
                    <input id="ativo" name="ativo" type="radio" value="1" <?php if ($this->input->post('ativo') == 1) echo "checked='true'" ?> />
                    <label for="inativo">Não</label>
                    <input id="inativo" name="ativo" type="radio" value="0"<?php if ($this->input->post('ativo') == 0) echo "checked='true'" ?>/>
                </div>
                <br class="clear"/>
                <br class="clear"/>

                <div>
                    Conteúdo post
                </div>
                <div>
                    <script type="text/javascript" src="<?php echo base_url('assets/js/tinymce/tinymce.min.js'); ?>"></script>
                    <script type="text/javascript">
                        tinymce.init({
                            selector: "textarea",
                            plugins: [
                                "advlist autolink autosave link image lists charmap print preview hr anchor pagebreak spellchecker",
                                "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
                                "table contextmenu directionality emoticons template textcolor paste fullpage textcolor"
                            ],
                            image_list: '<?php echo site_url("admin/upload/image_list"); ?>',
                            relative_urls: false,
                            toolbar1: "newdocument fullpage | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | styleselect formatselect fontselect fontsizeselect",
                            toolbar2: "cut copy paste | searchreplace | bullist numlist | outdent indent blockquote | undo redo | link unlink anchor image media code | inserttime preview | forecolor backcolor",
                            toolbar3: "table | hr removeformat | subscript superscript | charmap emoticons | print fullscreen | ltr rtl | spellchecker | visualchars visualblocks nonbreaking template pagebreak restoredraft",
                            menubar: false,
                            language: "pt_BR",
                            resize: false,
                            toolbar_items_size: 'small',
                            style_formats: [
                                {title: 'Bold text', inline: 'b'},
                                {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
                                {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
                                {title: 'Example 1', inline: 'span', classes: 'example1'},
                                {title: 'Example 2', inline: 'span', classes: 'example2'},
                                {title: 'Table styles'},
                                {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
                            ],
                            templates: [
                                {title: 'Test template 1', content: 'Test 1'},
                                {title: 'Test template 2', content: 'Test 2'}
                            ]
                        });
                    </script>

                    <textarea name="conteudo" style="width: 100%; height: 500px;">
                        <?php echo $this->input->post('conteudo'); ?>
                    </textarea>
                </div>
                <br/>
            </form>
            <footer class="node-footer" style="text-align: right; padding-bottom: 10px;">
                <ul class="links inline">
                    <li class="comment-add first"><a title="cancelar" href="javascript:resetForm();">Cancelar</a></li>
                    <li class="node-readmore"><a id="upload" title="upload" target="_blank" href="<?php echo site_url('admin/upload/index') ?>">Upload imagem</a></li>
                    <li class="node-readmore last"><a title="salvar" rel="tag" href="javascript:submitForm();">Salvar</a></li>
                </ul>
            </footer>
        </div>
    </div>
</div>
<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('#id_menu_principal').change(function() {
            var idMenuPrincipal = jQuery(this).find('option:selected').val();
            var $containerLoad = jQuery('#container-load');
            if (idMenuPrincipal != '') {
                showLoad($containerLoad);
                jQuery.ajax({
                    type: 'GET',
                    url: "<?php echo site_url('admin/post/ajax_getMenus') ?>".concat('/', idMenuPrincipal),
                    success: function(html) {
                        jQuery('#menu').html(html);
                        hideLoad($containerLoad);
                    },
                    error: function(err) {
                        hideLoad($containerLoad);
                        alert(err);
                    }
                });
            }
        })
<?php
if (!$editar && empty($id_menu_principal)) {
    echo ".trigger('change')";
}
?>;


    });
    function submitForm() {
        jQuery('#content').val(tinyMCE.get('conteudo').getBody().innerHTML);
        jQuery('#form').submit();
    }

    function resetForm() {
        jQuery('#form')[0].reset();
    }
</script>
